package com.fishsticks;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/** 
 * First activity started in To-Do List app
 * Lets the user register or login
 */
public class LoginActivity extends Activity 
{
	private TextView usernameField, passwordField;
	private Button registerButton, loginButton;
	private AlertDialog dialog;
	
    /** 
     * Called when the activity is first created. 
     * Creates button click listeners and intents to
     * switch between login/register activities
     * */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);
        
        registerButton = (Button) findViewById(R.id.registerButtonLoginPage);
        loginButton = (Button) findViewById(R.id.loginButtonLoginPage);
        
        usernameField = (TextView) findViewById(R.id.usernameFieldLoginPage);
        passwordField = (TextView) findViewById(R.id.passwordFieldLoginPage);
        
        dialog = new AlertDialog.Builder(this).create();
        
        
        
       
        registerButton.setOnClickListener(new OnClickListener() 
        {
			@Override
			public void onClick(View arg0) 
			{
				//Switches to register screen
				Intent i = new Intent(getApplicationContext(), RegisterActivity.class);		
				startActivity(i);
			}
        });
        
        
        
        
        
        loginButton.setOnClickListener(new OnClickListener() 
        {
			@Override
			public void onClick(View arg0) 
			{
				//check for blank fields
				if(checkValidFields(usernameField.getText().toString(), passwordField.getText().toString()))
				{
					//check login credentials
					//TODO: FIX THIS
					if(checkCredentials(usernameField.getText().toString(), passwordField.getText().toString()))
					{
						//Show dialog box for successful login
						dialog.setTitle("Success");
				    	dialog.setMessage("Login successful! *do more stuff later*");
				    	dialog.setButton("Woohoo!", new android.content.DialogInterface.OnClickListener() 
				    	{
							@Override
							public void onClick(DialogInterface dialog, int which) 
							{
								dialog.dismiss();
							}
				    		
				    	});
				    	dialog.show();
					}
					
					//Show alert for incorrect credentials
					else
					{
						dialog.setTitle("Error");
				    	dialog.setMessage("The username or password is invalid.");
				    	dialog.setButton("Okay", new android.content.DialogInterface.OnClickListener() 
				    	{
				    		//Hides dialog after user clicks "ok" button
							@Override
							public void onClick(DialogInterface dialog, int which) 
							{
								dialog.dismiss();
							}
				    		
				    	});
				    	dialog.show();
					}
				}
				
				
				//Show alert for blank fields
				else
				{
			    	dialog.setTitle("Fields are blank");
			    	dialog.setMessage("The username and password fields cannot be blank.");
			    	dialog.setButton("Okay", new android.content.DialogInterface.OnClickListener() 
			    	{
			    		//Hides dialog after user clicks "ok" button
						@Override
						public void onClick(DialogInterface dialog, int which) 
						{
							dialog.dismiss();
						}
			    		
			    	});
			    	dialog.show();
				}
			}
        });
    }
    
    
    
    
    
    
    /**
     * Checks the log in credentials with database manager
     * 
     * @param username The username to check
     * @param password The password to check
     * @return A boolean representing whether or not the login
     *         credentials are valid in the DB
     */
    public boolean checkCredentials(String username, String password)
    {
    	//TODO: FIX THIS METHOD
    	return true;
    }
    
    
    
    
    
    /**
     * Checks if the username or password fields are blank 
     * and alerts the user if they are
     * 
     * @param username The username to check
     * @param password The password to check
     * @return A boolean representing whether or not the fields are blank
     */
    public boolean checkValidFields(String username, String password)
    {
    	
    	if(username.length() != 0)
    	{
    		if(password.length() != 0)
    			return true;
    		
    		else
    			return false;
    	}
    	
    	else
    		return false;
    }
}